package com.xpsoft.oa.action.info;


import java.util.List;
import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;

import java.lang.reflect.Type;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.reflect.TypeToken;

import com.xpsoft.core.Constants;
import com.xpsoft.core.command.QueryFilter;
import com.xpsoft.core.web.action.BaseAction;


import com.xpsoft.oa.model.info.CompanyRole;
import com.xpsoft.oa.model.system.FileAttach;
import com.xpsoft.oa.service.info.CompanyRoleService;
import com.xpsoft.oa.service.system.FileAttachService;

import flexjson.DateTransformer;
import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class CompanyRoleAction extends BaseAction{
	@Resource
	private CompanyRoleService companyRoleService;
	@Resource
	private FileAttachService fileAttachService;
	private CompanyRole companyRole;
	
	private Long id;

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public CompanyRole getCompanyRole() {
		return companyRole;
	}

	public void setCompanyRole(CompanyRole companyRole) {
		this.companyRole = companyRole;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		
		QueryFilter filter=new QueryFilter(getRequest());
		List<CompanyRole> list= companyRoleService.getAll(filter);
		
		Type type=new TypeToken<List<CompanyRole>>(){}.getType();
		StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
		.append(filter.getPagingBean().getTotalItems()).append(",result:");
		
		Gson gson=new GsonBuilder().excludeFieldsWithoutExposeAnnotation().setDateFormat(Constants.DATE_FORMAT_YMD).create();
		buff.append(gson.toJson(list, type));
		buff.append("}");
		
		jsonString=buff.toString();
		
		return SUCCESS;
	}
	/**
	 * 批量删除
	 * @return
	 */
	public String multiDel(){
		
		String[]ids=getRequest().getParameterValues("ids");
		if(ids!=null){
			for(String id:ids){
				companyRoleService.remove(new Long(id));
			}
		}
		
		jsonString="{success:true}";
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		CompanyRole companyRole=companyRoleService.get(id);
		
		JSONSerializer serializer = new JSONSerializer();
		serializer.transform(new DateTransformer(
				"yyyy-MM-dd HH:mm:ss"), new String[] { "effectiveDate" });
		serializer.transform(new DateTransformer(
				"yyyy-MM-dd HH:mm:ss"), new String[] { "expirationDate" });

		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(serializer.exclude(new String[] { "class" }).include("treeType","attachFiles")
				.serialize(companyRole));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	
	public String getByTitle(){
		String title = getRequest().getParameter("title");
		CompanyRole companyRole=companyRoleService.getByTitle(title);
		JSONSerializer serializer = new JSONSerializer();
		serializer.transform(new DateTransformer(
				"yyyy-MM-dd HH:mm:ss"), new String[] { "effectiveDate" });
		serializer.transform(new DateTransformer(
				"yyyy-MM-dd HH:mm:ss"), new String[] { "expirationDate" });

		StringBuffer sb = new StringBuffer("{success:true,data:");
		if(companyRole!=null){
			sb.append(serializer.exclude(new String[] { "class" }).include("treeType","attachFiles")
					.serialize(companyRole));
		}else{
			sb.append("''");
		}
		sb.append("}");
		setJsonString(sb.toString());
		return SUCCESS;
	}
	
	/**
	 * 添加及保存操作
	 */
	public String save(){
		String attachIds = getRequest().getParameter("attachIds");
		if (StringUtils.isNotEmpty(attachIds)) {
		       this.companyRole.getAttachFiles().clear();
		       String[] fIds = attachIds.split(",");
		       for (int i = 0; i < fIds.length; i++) {
		         FileAttach fileAttach = (FileAttach)this.fileAttachService.get(new Long(fIds[i]));
		         this.companyRole.getAttachFiles().add(fileAttach);
		       }
	     }
		companyRoleService.save(companyRole);
		setJsonString("{success:true}");
		return SUCCESS;
	}
}
